// 引入接口文件
import { queryAllAddress, down, importAccount,deleteData ,regionArea,getProduct} from './accountListServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { download, makeAddress } from 'utils/util'
// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  xxx: '1',
  exportLoading: false,
  biz: {
    queryForm: {},
    selectedRows: []
  },
  uploadFileObj: {//导入导出
    visible: false,
    loading: false,
    templateType: 1,
    productType: '',
  },
  imageUrls: [],
  modalObj: {
    visible: false,
  },
  areas:[],
  products:[]
}

// 声明module
const tmpModule = {
  
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname, query}) => {
        // 页面路由匹配时执行
        if ('' + tmpModule.pathname === '' + pathname) {
          // 执行业务操作
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
          dispatch({type: 'getAreaList', payload: {}})
          dispatch({type: 'regionArea', payload: {}})
        } // end if
      }) // end listen
    },
  },
  
  // 方法列表
  effects: {
    *regionArea({payload},{put,call,select}){
			let {params,areas}=yield select(state => state[tmpModule.namespace])
					const {success,data}=yield call(regionArea)
					if(success){
					
						yield put({type:'updateStore',payload:{area1:data,areas}})
						yield put({type:'getProduct',payload:{}})
					}
				},
		*getProduct({payload},{put,call,select}){
			let {products,biz} = yield select(state => state[tmpModule.namespace])
				const { data, success } = yield call(getProduct,{cityCode:biz.queryForm.city});
				if(success){
					products=data
					yield put({ type: 'updateStore',payload:{products}})
				}
		},
    // 查询记录

    * getAreaList ({payload}, {put, call, select}) {
      const {data, success} = yield call(queryAllAddress)
      if (success) {
        let areaList = []
        let arr = [data[0]]
        let attr = [data[data.length - 1]]
        data[0] = {
          label: '北京',
          value: '110100',
          children: arr
        }
        data[data.length - 1] = {
          label: '澳门',
          value: '820000',
          children: attr
        }
        //去掉第三级，
        areaList = makeAddress(data, 2)
        yield put({type: 'updateStore', payload: {areaList}})
      }
    },
    * down ({payload}, {put, call, select}) {
      try {
        let tmpState = yield select(state => state[tmpModule.namespace])
        let {queryForm} = tmpState.biz
        const {data, success} = yield call(down)
        yield put({type: 'updateStore', payload: {exportLoading: false}})
        if (success) {
          // yield put({ type: 'updateStore', payload: {  data } });
          let url = data.url || ''
          if (url) {
            download(url)
          }
          message.success('操作成功')
          // window.open(data.url)
          
        }
      } catch (e) {
        console.error(e)
      }
    },
    
    * importAccount ({payload}, {put, call, select}) {
      let {uploadFileObj, imageUrls} = yield select(state => state[tmpModule.namespace])
      let fileUrl = imageUrls[0] && imageUrls[0].url || ''
      if (!fileUrl) {
        message.error('请上传文件')
        return false
      }
      uploadFileObj.loading = true
      yield put({type: 'updateStore', payload: {uploadFileObj}})
      let params = {
        fileUrl,
      }
      try {
        let state = yield select(state => state[tmpModule.namespace])
        let {biz} = yield select(d => d[tmpModule.namespace])
        const {data, success, msg} = yield call(importAccount, {...params})
        if (success === true) {
          message.success('操作成功')
          biz.refreshListDatas()
        }
      } catch (e) {
        message.error(msg)
        return false
      }
      uploadFileObj.visible = false
      uploadFileObj.loading = false
      // yield put({ type: 'updateStore', payload: {uploadFileObj}});
    },
    *deleteData({payload},{put,call,select}){
      let {success}=yield call(deleteData,payload.id)
      let {biz} = yield select(d => d[tmpModule.namespace])
      if(success){
        message.success('删除成功',()=>{
          biz.refreshListDatas()
        })
      }
    }
  },
  
  // 存入状态机
  reducers: {
    updateStore (preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
